import cTitle from "components/title";
import emptyImage from "@/assets/images/new_diy/image.png";
import { Toast } from "vant";
import { Dialog } from "vant";
import { Image } from "vant";
export default {
  data() {
    return {
      emptyImage,
      cstoreId: null, //	商户ID
      authType: 0, //权限
      timeType: "", //选择日期
      longitude: "",
      latitude: "",
      groupList: {}, //群数量
      storeInfo: {},
      storeList: [],
      slideBanner: [], //广告轮播图
      distanceInex: 2, //选择距离的索引
      distance: 3, //默认3KM
      distanceItem: 3,
      distanceList: [1, 2, 3, 4, 5],
      authTypeName: "",
      distanceShow: false, //选择距离开启
      businessShow: false, //切换商户开启
      couponShow: false,
      couponText: "",
      couponSn: "", //核销券码
      shopList: [
        //门店经营列表
        {
          text: "扫码验券",
          img: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/shop1.png",
          navTo: "couponCode"
        },
        {
          text: "输码验券",
          img: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/shop2.png",
          navTo: "couponShow"
        },
        {
          text: "核销明细",
          img: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/shop3.png",
          navTo: "verificationDetails"
        },
        {
          text: "商品管理",
          img: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/shop4.png",
          navTo: "commodity"
        },
        {
          text: "售卖明细",
          img: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/shop5.png",
          navTo: "sellDetail"
        },
        {
          text: "客户评价",
          img: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/shop6.png",
          navTo: "clientEvaluation"
        },
        {
          text: "核销员管理",
          img: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/shop7.png",
          navTo: "verifierList"
        },
        {
          text: "收益管理",
          img: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/shop8.png",
          navTo: "incomeDetail"
        }
      ],
      shopVerifierList: [
        //门店经营列表
        {
          text: "扫码验券",
          img: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/shop1.png",
          navTo: "couponCode"
        },
        {
          text: "输码验券",
          img: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/shop2.png",
          navTo: "couponShow"
        }
      ],
      statsList: [
        {
          price: 3,
          text: "核销订单数",
          code: ""
        },
        {
          price: 0,
          text: "待核销订单数",
          code: ""
        },
        {
          price: 0,
          text: "支付订单数",
          code: ""
        },
        {
          price: 0,
          text: "退款订单数",
          code: ""
        },
        {
          price: 0,
          text: "会员人数",
          code: ""
        },
        {
          price: 0,
          text: "在售商品数",
          code: ""
        }
      ],
      swiperOption: {
        autoplay: false,
        // loop: true,   loop模式第二轮循环的时候第一张会闪烁
        delay: 3000,
        observer: true,
        pagination: {
          el: ".swiper-pagination"
        }
      }
    };
  },
  activated() {
    this.cstoreId = this.$route.query.cstoreId;
    this.fun
      .getLocation()
      .then(res => {
        let p = this.fun.bd_encrypt(res.point.lng, res.point.lat);
        this.latitude = p.lat;
        this.longitude = p.lng;
        this.businessData();
      })
      .catch(err => {
        console.log(err);
        // setTimeout(() => {
        //   this.$router.go(-1);
        // }, 500);
      });
  },
  methods: {
    businessData() {
      let json = {
        cstore_id: this.cstoreId,
        distance: this.distance,
        longitude: Number(this.longitude),
        latitude: Number(this.latitude),
        time_type: this.timeType
      };
      $http.post("plugin.coupon-store.frontend.store-manage.index", json, "正在获取").then(
        response => {
          if (response.result == 1) {
            let res = response.data;
            let count = res.count || {};
            // let statsList = this.statsList;
            Object.keys(count).forEach((item, index) => {
              //循环数据统计对象
              this.statsList[index].code = item;
              this.statsList[index].price = count[item];
            });
            this.authTypeName = res.auth_type_name;
            this.authType = res.auth_type;
            this.slideBanner = res.slide;
            this.groupList = res.group_list;
            this.storeList = res.store_list;
            this.cstoreId = res.store.id;
            this.storeInfo = res.store;
            console.log(res);
          } else {
            // this.$dialog.alert({message:response.msg});
            //Toast(response.msg);
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    },
    onClose(end) {
      if (end === "distance") {
        this.distanceShow = false;
      } else if (end === "couponCode") {
        this.couponShow = false;
      } else {
        this.businessShow = false;
      }
    },
    onOpen(start) {
      if (start === "distance") {
        this.distanceShow = true;
      } else if (start === "couponCode") {
        this.couponShow = false;
      } else {
        this.businessShow = true;
      }
    },
    bindCoupon(value) {
      console.log(value);
      this.couponSn = value;
      // this.couponText = value;
    },
    onAffirm() {
      this.distanceShow = false;
      this.distance = this.distanceItem;
      this.businessData();
    },
    onCouponAffirm() {
      if (this.couponText) {
        Dialog.confirm({
          title: "提示",
          message: `是否确认核销消费券号：${this.couponSn}`
        })
          .then(() => {
            // on confirm
            let json = {
              cstore_id: this.cstoreId,
              coupon_sn: this.couponSn,
              request_type: 2
            };
            $http.post("plugin.coupon-store.frontend.store-manage.examineCoupon", json, "正在获取").then(
              response => {
                if (response.result == 1) {
                  // let res = response.data;
                  this.couponShow = false;
                  this.couponText = "";
                  Toast(response.msg);
                } else {
                  Dialog.alert({
                    title: "提示",
                    message: response.msg
                  }).then(() => {
                    // on close
                  });
                }
              },
              function(response) {
                // console.log(response);
                // error callback
              }
            );
          })
          .catch(() => {
            // on cancel
          });
      } else {
        Toast("请输入消费券编码");
      }
    },
    onActiveStore(id) {
      //切换商户
      // let id = event.currentTarget.dataset.id;
      this.businessShow = false;
      this.$router.replace(this.fun.getUrl("business", {}, { cstoreId: id }));
    },
    onDistance(...query) {
      //选择不同距离
      const [index, item] = [...query];
      this.distanceInex = index;
      this.distanceItem = item;
    },
    slideNavTo(navTo) {
      window.location.href = navTo;
    },
    timeBusiness(date) {
      if (this.timeType !== date) {
        //避免重复请求数据
        this.timeType = date;
        this.businessData();
      }
    },
    shopItemOn(navTo) {
      //跳转
      if (navTo === "couponShow") {
        this.couponShow = true;
      } else if (navTo === "couponCode") {
        let that = this;
        wx.config({
          // 配置信息, 即使不正确也能使用 wx.ready
          debug: false,
          appId: "",
          timestamp: 1,
          nonceStr: "",
          signature: "",
          jsApiList: []
        });
        wx.ready(function() {
          wx.scanQRCode({
            needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
            scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
            success: function(res) {
              // let result = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
              var result = res.resultStr;
              that.$router.push(that.fun.getUrl("verification", {}, { storeId: that.cstoreId, couponSn: result }));
            },
            fail: err => {
              console.log(err);
            }
          });
        });

        // this.$router.push(this.fun.getUrl("verification", {}, { storeId: this.cstoreId, couponSn: result }));
      } else {
        this.$router.push(this.fun.getUrl(navTo, {}, { storeId: this.cstoreId }));
      }
    }
  },
  components: {
    cTitle,
    VanImage: Image
  }
};
